package com.tstudy.laoshibang.base;

import org.apache.http.Header;

import com.google.gson.Gson;
import com.loopj.android.http.BaseJsonHttpResponseHandler;
import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.active.ActiveDetailFragment;
import com.tstudy.laoshibang.event.GroupMessageEvent;
import com.tstudy.laoshibang.mode.TGroup;
import com.tstudy.laoshibang.mode.response.GroupDetailResponse;
import com.tstudy.laoshibang.profile.MessageDetailFragment;
import com.tstudy.laoshibang.utils.CommonUtil;
import com.tstudy.laoshibang.utils.LogUtil;
import com.tstudy.laoshibang.weike.WeikeDetailPlayerFragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import de.greenrobot.event.EventBus;

public class NotificationReceiver extends BroadcastReceiver{

	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = intent.getBundleExtra(CONSTANT.ARGS.BUNDLE);
		int assoId = bundle.getInt(CONSTANT.ARGS.ASSO_ID, -1);
		String url = bundle.getString(CONSTANT.ARGS.URL);
		int bussType = bundle.getInt(CONSTANT.ARGS.BUSS_TYPE, -1);
		String groupId = bundle.getString(CONSTANT.ARGS.GROUP_ID);
		String groupName = bundle.getString(CONSTANT.ARGS.GROUP_NAME);
		LogUtil.d("-- onReceiver getui push message initArgs assoId==" + assoId + "--bussType==" + bussType);
		String value = "";
		EventBus.getDefault().post(new GroupMessageEvent());
		if (CommonUtil.isRunningForeground(BaseApplication.mContext)) {
			switch (bussType) {
			case CONSTANT.PUSH_BUSINESS_TYPE_ACTIVE:
				value = BaseApplication.getResString(R.string.active_list_title);
				ActiveDetailFragment.add(((BaseActivity)BaseApplication.getTopActivity()).mRootId, assoId,-1,groupId);
				break;
			case CONSTANT.PUSH_BUSINESS_TYPE_SYSTEM:
				value = BaseApplication.getResString(R.string.message_list_title);
				MessageDetailFragment.add(((BaseActivity)BaseApplication.getTopActivity()).mRootId, assoId,0,url,null);
				break;
			case CONSTANT.PUSH_BUSINESS_TYPE_WEIKE:
				value = BaseApplication.getResString(R.string.weike_list_title);
				WeikeDetailPlayerFragment.add(((BaseActivity)BaseApplication.getTopActivity()).mRootId, assoId,-1,groupId);
				break;
			case CONSTANT.PUSH_BUSINESS_TYPE_GROUP_AGREE:
				value = CONSTANT.EVENT_VALUE.GROUP_AGREE;
				BaseApplication.changeGroupAction(new TGroup(groupId, groupName));
//				getGroupProfile(groupId);
				break;
			case CONSTANT.PUSH_BUSINESS_TYPE_GROUP_REJECT:
//				value = CONSTANT.EVENT_VALUE.GROUP_REJECT;
//				MessageDetailFragment.add(((BaseActivity)BaseApplication.getTopActivity()).mRootId, assoId,0,url,null);
				break;
			case CONSTANT.PUSH_BUSINESS_TYPE_GROUP_REMOVE:
				value = CONSTANT.EVENT_VALUE.GROUP_REMOVE;
				if (BaseApplication.mCurrentGroup != null && groupId != null && groupId.equals(BaseApplication.mCurrentGroup.groupId)) {
					getGroupProfile(null);;//被移出当前群组，默认回到官方群组
				}else{
					MessageDetailFragment.add(((BaseActivity)BaseApplication.getTopActivity()).mRootId, assoId,0,url,null);
				}
				break;
			case CONSTANT.PUSH_BUSINESS_TYPE_ADMIN:
				value = BaseApplication.getResString(R.string.message_admin_title);
				MessageDetailFragment.add(((BaseActivity)BaseApplication.getTopActivity()).mRootId, assoId,0,url,null);
				break;
			default:
				break;
			}
			CommonUtil.umengEvent(CONSTANT.EVENT_ID.PUSH_MESSAGE, "类型", value);
		}else{
			IndexActivity.messageShow(context,bundle);
		}
	}
	
	private void getGroupProfile(String groupId){
		BaseJsonHandler<GroupDetailResponse> response = new BaseJsonHandler<GroupDetailResponse>() {
			public void onStart() {
			}
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					String rawJsonResponse, GroupDetailResponse response) {
				super.onSuccess(statusCode, headers, rawJsonResponse, response);
				if (CommonUtil.responseSuccess(response)) {
					if (response.data != null) {
						BaseApplication.changeGroupAction(response.data);
					}
				}else {
                    BaseApplication.showToast(response.getErrMsg());
                }
			}
			@Override
			public void onFailure(int statusCode, Header[] headers,
					Throwable throwable, String rawJsonData,
					GroupDetailResponse errorResponse) {
				super.onFailure(statusCode, headers, throwable, rawJsonData, errorResponse);
			}
			@Override
			protected GroupDetailResponse parseResponse(String rawJsonData, boolean isFailure)
					throws Throwable {
				return new Gson().fromJson(rawJsonData,GroupDetailResponse.class);
			}
		};
		if (TextUtils.isEmpty(groupId)) {//获取官方群组
			HttpManager.getInstance().officialGroup(BaseApplication.mUserNo,response);
		}else{
			HttpManager.getInstance().getGroupDetail(BaseApplication.mUserNo, groupId, response);
		}
	}
	
	 abstract public  class BaseJsonHandler<T> extends BaseJsonHttpResponseHandler<T> {
	        @Override
	        public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, T response) {
	            
	        }

	        @Override
	        public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, T errorResponse) {
	            BaseApplication.showToast("数据请求异常");
	        }

	        @Override
	        public void onStart() {
	            super.onStart();
	            LogUtil.d("onStart showloading::" + this);
//	            showLoadingProgressDialog();
	        }
	    }

}
